/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import com.gr.context.ContextController;
import com.gr.context.ContextManager;
import com.gr.context.Profile;
import java.io.*;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.String;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author Binh
 */
public class login_action extends HttpServlet {
    String ip ="123.24.204.18";
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet login_action</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet login_action at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
   public void doGet (HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
        response.setContentType("text/html");
        String name = request.getParameter("txt_user");
        String pass = request.getParameter("txt_pass");
//        String qs = request.getQueryString();
//        Enumeration e = request.getHeaderNames();
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<B>Hello </B>"+ name);
        out.println("<p>"+pass+"</p>");
//        out.println ("<B>Query String:</B>"+qs);
//        out.println("<br> Header </br>"+ e);
        out.println("</BODY></HTML>");
        out.println("<script type=\"text/javascript\"> setTimeout(\"location.href=\'Index.html\' ;\",1000) </script>");
     }
  
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
           request.setCharacterEncoding("UTF-8");
           response.setCharacterEncoding("UTF-8");
           String name = request.getParameter("txt_user");
           String pass = request.getParameter("txt_pass");
           PrintWriter out = response.getWriter();
           int check = Profile.checkProfile(name,pass);
           ContextController cc = new ContextController(check);
           cc.gainLog();
           cc.loadUserInterest();
           cc.updateHistory();
           //int check =0;
           if(check!=0){
               HttpSession session = request.getSession(true);
               session.setAttribute("id", check);
               try {
                   ContextManager cm=new ContextManager(check,ip);
                   out.println("<HTML>");
                    out.println("<HEAD><meta  http-equiv='Content-Type'  content='text/html; charset=UTF-8'/><TITLE>Context-based IRS</TITLE><link href=\"main.css\" rel=\"stylesheet\" type=\"text/css\" /></HEAD>");
                    out.println("<BODY>");
                    out.println("<div id=\"form\" name=\"form\" style=\"background-color: #e9e5e5;padding-top:30px;height:100px;text-align:center;font-size:large border-bottom:black\">");
                    out.println("<form method=post name=\"search2\" action=\"result\">");
                    out.println("<p><input type=\"text\" value=\"\" name=\"txt_query\" id=\"txt_query\" style=\"width:450px;	height:30px;font-size: large;\"> <input type=\"submit\" value=\"Search\"></p>");
                    out.println("<input type=\"radio\" name=\"search_type\" id=\"txt_context\" value=\"txt_context\" checked=\"checked\"/>Context-based search<input type=\"radio\" name=\"search_type\" id=\"txt_noncontext\" value=\"txt_noncontext\" />Non-context-based search");
                    out.println("<input type=\"radio\" name=\"cluster\" id=\"txt_non cluster\" value=\"txt_noncluster\" checked=\"checked\"/>Non-cluster results<input type=\"radio\" name=\"cluster\" id=\"txt_cluster\" value=\"txt_cluster\" />Cluster results");
                    out.println("</form>");
                    out.println("</div>");
                    
                    out.println("<div id=\"left_content\" style=\"padding-left:5px;margin_top:10px;float:left;width:18%\"><table><tr><td>Xinc chào: </td><td><b>"+cm.getProfile().getName()+" </b></td></tr><tr><tr><td>Vị trí :</td><td>"+cm.getLocation().city+"</td></tr><tr><td></td><td><form method =\"post\" action=\"sign_out\"> <input type=\"submit\" value=\"Thoát\"></form></td></tr></table><hr>");
                    if(session!=null){
                        String[] interestList = cm.getProfile().getInterest();
                        out.println("<b>Sở thích chính</b><br>");
                        for(int i=0;i<interestList.length;i++)
                        if(interestList[i]!=null) out.println(interestList[i]+"<br>");

                        out.println("<hr></div>"); 
                    }
                    out.println("<div id=\"display\" name=\"display\" style=\"padding-left: 5px;padding-top:30px; float;right\">");
                    out.println("</div>");
                    out.println("</BODY></HTML>");
               } catch (Exception ex) {
                   ex.printStackTrace();
               }
       }else{
           out.println("<HTML>");
           out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
           out.println("<BODY>");
           out.println("<B>Sign in failed!</B>"+name+"--"+pass);
   //        out.println ("<B>Query String:</B>"+qs);
   //        out.println("<br> Header </br>"+ e);
           out.println("</BODY></HTML>"); 
           out.println("<script type=\"text/javascript\"> setTimeout(\"location.href=\'Index.html\' ;\",1000) </script>");
           }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
